<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/17 0017
 * Time: 18:48
 */
namespace console\controllers;

use yii\console\Controller;
use frontend\models\Order;


class TaskController extends Controller
{
    public function actionClearOuttimeOrder(){
        //设置脚本执行的最大时间 0 表示没有限制
        set_time_limit(0);
        //失效无限循环
        while (true) {
            //找到所有的超时订单 并且更新状态为0
            $time = time() - 3600;
            //echo $time;
            //$rs = Order::updateAll(['status'=>0],['<','update_time',$time]);
            $orders = Order::find()->where(['<', 'update_time', $time])->andWhere(['status' => 1])->all();
            //var_dump($rs);
            foreach ($orders as $order) {
                /** @var $order \frontend\models\Order */
                $order->status = 0;
                $order->update_time = time();
                $rs = $order->save();
                echo 'order ' . $order->id . ' has been done at ' . date('Y-M-D h:i:s')."\r\n";
            }
            //每过2秒执行一次
            sleep(2);
        }

    }
}